import { notification } from 'antd';

// 全局通知配置
const notificationConfig = {
  placement: 'topRight',
  duration: 4.5,
  maxCount: 3,
};

// 初始化通知配置
export const initNotification = () => {
  notification.config(notificationConfig);
};

// 通知类型枚举
export const NOTIFICATION_TYPES = {
  SUCCESS: 'success',
  ERROR: 'error',
  WARNING: 'warning',
  INFO: 'info',
  ALERT: 'alert', // 预警通知
  URGENT: 'urgent', // 紧急通知
};

// 通知消息模板
export const NOTIFICATION_MESSAGES = {
  // 审批相关
  APPROVAL_PENDING: '您有新的审批待处理',
  APPROVAL_APPROVED: '您的申请已通过审批',
  APPROVAL_REJECTED: '您的申请被拒绝',
  
  // 考勤相关
  ATTENDANCE_REMINDER: '请及时提交今日考勤',
  ATTENDANCE_SUBMITTED: '考勤数据提交成功',
  
  // 项目相关
  PROJECT_ASSIGNED: '您被分配到新项目',
  PROJECT_DEADLINE: '项目即将到期，请及时处理',
  
  // 系统相关
  SYSTEM_MAINTENANCE: '系统将在今晚进行维护',
  SYSTEM_UPDATE: '系统已更新到最新版本',
  
  // 预警相关
  ALERT_BUDGET_EXCEED: '项目预算超支预警',
  ALERT_DEADLINE_APPROACHING: '项目截止日期临近预警',
  ALERT_ATTENDANCE_ABNORMAL: '考勤异常预警',
  ALERT_CONTRACT_EXPIRING: '合同即将到期预警',
  ALERT_PAYMENT_OVERDUE: '付款逾期预警',
  ALERT_SYSTEM_ERROR: '系统错误预警',
  ALERT_DATA_BACKUP: '数据备份预警',
  ALERT_SECURITY_THREAT: '安全威胁预警',
  ALERT_PERFORMANCE_ISSUE: '性能问题预警',
  ALERT_RESOURCE_SHORTAGE: '资源不足预警',
  
  // 紧急通知
  URGENT_SYSTEM_DOWN: '系统紧急停机通知',
  URGENT_DATA_LOSS: '数据丢失紧急通知',
  URGENT_SECURITY_BREACH: '安全漏洞紧急通知',
  URGENT_CRITICAL_ERROR: '严重错误紧急通知',
  
  // 通用
  WELCOME: '欢迎使用项目管理系统',
  LOGOUT: '您已安全退出系统',
};

// 显示通知的封装函数
export const showNotification = (type, message, description = '', duration = 4.5) => {
  const config = {
    message,
    description,
    duration,
    placement: 'topRight',
  };

  switch (type) {
    case NOTIFICATION_TYPES.SUCCESS:
      notification.success(config);
      break;
    case NOTIFICATION_TYPES.ERROR:
      notification.error(config);
      break;
    case NOTIFICATION_TYPES.WARNING:
      notification.warning(config);
      break;
    case NOTIFICATION_TYPES.ALERT:
      // 预警通知使用warning样式，但添加特殊标识
      config.style = { borderLeft: '4px solid #faad14' };
      notification.warning(config);
      break;
    case NOTIFICATION_TYPES.URGENT:
      // 紧急通知使用error样式，但添加特殊标识
      config.style = { borderLeft: '4px solid #ff4d4f' };
      notification.error(config);
      break;
    case NOTIFICATION_TYPES.INFO:
    default:
      notification.info(config);
      break;
  }
};

// 快捷方法
export const showSuccess = (message, description) => 
  showNotification(NOTIFICATION_TYPES.SUCCESS, message, description);

export const showError = (message, description) => 
  showNotification(NOTIFICATION_TYPES.ERROR, message, description);

export const showWarning = (message, description) => 
  showNotification(NOTIFICATION_TYPES.WARNING, message, description);

export const showInfo = (message, description) => 
  showNotification(NOTIFICATION_TYPES.INFO, message, description);

export const showAlert = (message, description) => 
  showNotification(NOTIFICATION_TYPES.ALERT, message, description);

export const showUrgent = (message, description) => 
  showNotification(NOTIFICATION_TYPES.URGENT, message, description);

// 带操作按钮的通知
export const showNotificationWithAction = (type, message, description, actions = []) => {
  const config = {
    message,
    description,
    duration: 0, // 不自动关闭
    placement: 'topRight',
    btn: actions.length > 0 ? (
      <div>
        {actions.map((action, index) => (
          <button
            key={index}
            onClick={action.onClick}
            style={{
              marginRight: 8,
              padding: '4px 8px',
              border: '1px solid #d9d9d9',
              borderRadius: '4px',
              background: action.type === 'primary' ? '#1890ff' : '#fff',
              color: action.type === 'primary' ? '#fff' : '#000',
              cursor: 'pointer',
            }}
          >
            {action.text}
          </button>
        ))}
      </div>
    ) : null,
  };

  switch (type) {
    case NOTIFICATION_TYPES.SUCCESS:
      notification.success(config);
      break;
    case NOTIFICATION_TYPES.ERROR:
      notification.error(config);
      break;
    case NOTIFICATION_TYPES.WARNING:
      notification.warning(config);
      break;
    case NOTIFICATION_TYPES.ALERT:
      config.style = { borderLeft: '4px solid #faad14' };
      notification.warning(config);
      break;
    case NOTIFICATION_TYPES.URGENT:
      config.style = { borderLeft: '4px solid #ff4d4f' };
      notification.error(config);
      break;
    case NOTIFICATION_TYPES.INFO:
    default:
      notification.info(config);
      break;
  }
};

export default {
  initNotification,
  showNotification,
  showSuccess,
  showError,
  showWarning,
  showInfo,
  showAlert,
  showUrgent,
  showNotificationWithAction,
  NOTIFICATION_TYPES,
  NOTIFICATION_MESSAGES,
};
